	(function(){
		var obj={};
		obj.ajaxRequest=function(type,url,data,headers,context,callback){
			$.ajax({type:type,url:url,data:JSON.stringify(data),headers:headers,context:context})
			.done(callback);
		};
		window.j=obj;
	})();
	
	
	
	$(function(){
		$("#submit").on("click",function(e){
			e.preventDefault();					
			var d={};
			$("#formId").find('input').each(function(){d[this.name]=this.value})
			j.ajaxRequest("POST","addCustomerByAjax",d,{"content-Type":"application/json"},this,function(customers){
				render(customers);
			});
		})
        function render(customers){
        	$("#tbody").empty();
            var tBody = $("#tbody"); 
            for(var i = 0;i<customers.length;i++){
             	var id = customers[i].id;
             	var switchSex = customers[i].sex=="true"?"male":"female";
                $("#forClone").clone(true)
                .attr("id","trId")
                .find(".name").text(customers[i].cname).end()
                .find(".sex").text(switchSex).end()
                .find(".sal").text(customers[i].sal).end()
                .find(".birth").text(formatDateTime(customers[i].birth)).end()
                .find(".delete").attr("id",customers[i].id).end()
                .find(".update").attr("id",customers[i].id).end()
                .appendTo(tBody);
            }
        }
		 $(".delete").on("click",function(){
			var tr = $(this).parent();
	        var tt = $(this).attr("id");
         	var customer = {id:tt};
			j.ajaxRequest("POST","delete",customer,{"content-Type":"application/json"},this,function(customers){
				render(customers);
			});	
				
				
         })
        
         $(".update").on("click",function(){
        	var tr = $(this).parent();
        	var tt = $(this).attr("id");
        	var name= tr.find("td:nth(1)").text()
        	var customer = {id:tt};
			j.ajaxRequest("POST","find",customer,{"content-Type":"application/json"},this,function(customer){
				$("#modelid").attr("value",customer.id);
				$("#modelname").attr("value",customer.cname);
				$("#modelsex").attr("value",customer.sex);
				$("#modelsal").attr("value",customer.sal);
				$("#modelbirth").attr("value",formatDateTime(customer.birth));
				$("#showFrame").modal("show");
			});	
			
			
        })
        function formatDateTime(inputTime) {    
            var date = new Date(inputTime);  
            var y = date.getFullYear();    
            var m = date.getMonth() + 1;    
            m = m < 10 ? ('0' + m) : m;    
            var d = date.getDate();    
            d = d < 10 ? ('0' + d) : d;     
            return y + '-' + m + '-' + d;    
        };
        $("#finalUpdate").on("click",function(){
        	var modalForm={};
			$("#modelFormId").find('input').each(function(){modalForm[this.name]=this.value})
			j.ajaxRequest("POST","update",modalForm,{"content-Type":"application/json"},this,function(customers){
				$("#showFrame").modal("hide");
				render(customers);
			});	
        	
        })
		function ajaxRequest(type,url,data,headers,context,callback){
			$.ajax({type:type,url:url,data:JSON.stringify(data),headers:headers,context:context})
			.done(callback);
		};
	})